<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
        .divX {
        position: absolute;
        top: 0px;
        right: 0px;
        text-align: right;
        padding-right: 3px;
        z-index: 200;
        -moz-border-radius: 20px;
        -webkit-border-radius: 20px;
        line-height: 10px;
        font-weight: bold;
        cursor: pointer;
        font-size: 10px;
    }

    .divimg {
        max-height: 220px;
        height: 220px;
        width: 258px;
        max-width: 258px;
        background-color: #fff;
        border: 1px dashed #e6e6e6;
        position: relative;
        text-align: center;
        font-size: 20px;
    }

        .divimg > img {
            padding-top: 10px;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">收款方</label>
            <div class="layui-input-block">
                <input type="text" name="charge_unit" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入收款方"
                    value="" class="layui-input">
                <tip>填写收款单位名称</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">银行卡号</label>
            <div class="layui-input-block">
                <input type="text" name="card_id" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入银行卡号"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">开户行</label>
            <div class="layui-input-block">
                <input type="text" name="bankname" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入开户行"
                    value="" class="layui-input">
            </div>
        </div>
        <!-- 上传图片 -->
        <!-- <input type="hidden" name="mainPic" />
        <input type="hidden" name="detailPic" /> -->
        <div class="layui-form-item">
            <!-- <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>上传主图(300kb内)</legend>
            </fieldset> -->
            <label class="layui-form-label required">上传收款码</label>
            <div class="layui-upload-drag" id="test10">
                <div class="layui-progress" lay-filter="lodingdemo" lay-showpercent="true">
                    <div class="layui-progress-bar" lay-percent="0%"></div>
                </div>
                <i class="layui-icon"></i>
                <p>点击上传，或将文件拖拽到此处</p>
                <hr />
            </div>
            <div class="layui-hide layui-input-block" id="uploadDemoView">
            </div>
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal layui-btn-radius" id="uploadBtn" lay-submit="">开始上传</button>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" >
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'upload'], function () {
            // 获取对应行的id
            var loc = location.href;
            var n1 = loc.length; //地址的总长度
            var n2 = loc.indexOf("="); //取得=号的位置
            var sid = decodeURI(loc.substr(n2 + 1, n1 - n2)); //从=号后面的内容

            var form = layui.form,
                layer = layui.layer,
                upload = layui.upload,
                $ = layui.$;
            // 上传图片
            var uploadMain = upload.render({
                elem: '#test10',
                url: '/upload'
                //改成您自己的上传接口
                    //, before: function (obj) {//上传前触发
                    //    //预读本地文件示例，不支持ie8
                    //    obj.preview(function (index, file, result) {
                    //        layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', result);
                    //        //$('#demo2').attr('src', result); //图片链接（base64）
                    //    });
                    //}
                    ,
                choose: function (obj) { //选择后触发
                    var files = this.files = obj.pushFile();
                    var view = layui.$('#uploadDemoView');
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        var _html = $(['<div class="divimg" style="float:left;color:red;">',
                            '<img style="max-width: 190px;max-height:200px"  id="viewImg' +
                            index + '" alt="上传成功后渲染"  src="' + result + '">',
                            '<div class="divX" >',
                            '<i class="layui-icon layui-icon-delete" style="font-size:20px"></i>',
                            '</div>', '</div>'
                        ].join(''));
                        _html.find(".divX").on('click', function () {
                            delete files[index]; //删除对应的文件
                            _html.remove();
                            uploadDetail.config.elem.next()[0].value = '';
                        })

                        layui.$('#uploadDemoView').removeClass('layui-hide');
                        view.removeClass('layui-hide');
                        _html.find(".divX").on('click', function () {
                            delete files[index]; //删除对应的文件
                            _html.remove();
                            uploadDetail.config.elem.next()[0].value = '';
                        })
                        view.append(_html);
                    });
                },
                auto: false //false将不触发before
                    //, bindAction: "#uploadBtn"
                    ,
                size: 1024 //单位kb
                    ,
                done: function (data) {
                    if (data.message != "") {
                        layer.msg(data.message);
                    }

                    if (data.type == 1) {
                        var fileUrl = data.resultdata;
                        $("input[name='mainPic']").val(fileUrl);
                    } else if (data.type == 3) {
                        return false;
                    }
                },
                progress: function (n, elem) {
                    var percent = n + '%' //获取进度百分比
                    element.progress('lodingdemo', percent); //可配合 layui 进度条元素使用
                }
            });

            $("#uploadBtn").click(function () {
                uploadMain.upload();
                return false;
            })


            //监听提交
            form.on('submit(saveBtn)', function (data) {
                var result = data.field;
                result.sid = sid;
                result = JSON.stringify(result);
                $.ajax({
                    url: "/schools",
                    method: "PUT",
                    data: result,
                    contentType: "application/json",
                    success: function (data) {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        parent.location.href = '/page/table.html';
                    },
                    error: function (err) {
                        layer.msg("服务器错误，请稍后重试!", {
                            icon: 2
                        });
                    }
                })
                return false;
            });

        });
    </script>
</body>

</html>